package com.gopay.bis.manager.bank.ebank;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.gdb.ebank.GdbB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 广发B2C网关支付对象封装
 * 
 * @GdbB2cManager.java
 * @author zhengxue
 * @2014-11-12 下午3:28:21 www.gopay.com.cn Inc.All rights reserved.
 */
public class GdbB2cManager implements BankManagerRemoteService {
    @Resource(name = "gdbB2cBankServiceClient")
    private BankService gdbB2cBankServiceClient;
    
    @Resource(name = "gdbWapBankServiceClient")
	private BankService gdbWapBankServiceClient;

    /**
     * B2C签名
     */
    public Object sign(BaseMessage msg) throws GopayException {
        GdbB2cMessage messageB2C = new GdbB2cMessage();
        // add by lilili 广发银行上送订单号要求固定长度20位
        //int a = (int)(Math.random()*(9999-1000+1))+1000;
        msg.setBankOrderId(msg.getOrderId());
        BeanUtils.copyProperties(msg, messageB2C);
        // 组装签名明文(商户号+订单号+金额)
        String plainMessage = buildPlainB2C(msg);
        messageB2C.setPlainMessage(plainMessage);
        messageB2C.setMccCode("4937");
        messageB2C.setGoodsType("L");
        messageB2C.setSendWay("1");
        messageB2C.setProductName("pay");
        messageB2C.setSecondMerName("国付宝信息科技有限公司");
        messageB2C.setWebsiteName("gopay");
        messageB2C.setNetAddress("https://bis.gopay.com.cn");
        messageB2C.setChannel("NP");
        messageB2C.setDomainName("bis.gopay.com.cn");

        BankLogUtils.printB2CManagerSignSendLog(messageB2C);
        // 调用远程接口请求签名数据
        String payUrl = (String) gdbB2cBankServiceClient.sign(messageB2C);
        messageB2C.setPayUrl(payUrl);
        BankLogUtils.printB2CManagerSignReturnLog(messageB2C);
        return messageB2C;
    }
    
	/**
	 * 构建B2C报文
	 * 
	 * @param message
	 *            广发银行B2C数据结构体
	 * @return String 商户签名源数据
	 */
	public String buildPlainB2C(BaseMessage message) {
		StringBuffer sb = new StringBuffer("");
		sb.append(message.getAmount()).append("||")
		.append(message.getMerID()).append("||")
		.append(message.getBankOrderId());
		return sb.toString();
	}

    /**
     * 验签
     */
    public Object verify(BaseMessage msg) throws GopayException {
        BankLogUtils.printB2CVerifyPlainLog(msg);
        BankLogUtils.printB2CVerifySignatureLog(msg, msg.getSignMessage());
        boolean isVerify;
        if (BisBankBusinessProd._11.value.equals(msg.getBankBusProd().value)) {
        	//wap网关交易
        	isVerify = (Boolean) this.gdbWapBankServiceClient.verify(msg);
        }else{
        	isVerify = (Boolean) this.gdbB2cBankServiceClient.verify(msg);
        }
        
        BankLogUtils.printB2CVerifyResultLog(msg, isVerify);
        return isVerify;
    }

    /**
     * 查询
     */
    public Object query(Object msg) throws GopayException {
		GdbB2cMessage messageB2C = (GdbB2cMessage) msg;
		// 组装签名明文(订单号+商户号)
		String plainMessage = "";
		if (BisBankBusinessProd._11.value.equals(messageB2C.getBankBusProd().value)) {
			//wap网关交易
			plainMessage = messageB2C.getBankOrderId() + "" + messageB2C.getMerID() + "";
			messageB2C.setPlainMessage(plainMessage);
			BankLogUtils.printB2CManagerQuerySendLog(messageB2C, "");
			messageB2C = (GdbB2cMessage) this.gdbWapBankServiceClient.query(messageB2C);
		} else {
			plainMessage = messageB2C.getBankOrderId() + "||" + messageB2C.getMerID() + "||";
			messageB2C.setPlainMessage(plainMessage);
			BankLogUtils.printB2CManagerQuerySendLog(messageB2C, "");
			messageB2C = (GdbB2cMessage) this.gdbB2cBankServiceClient.query(messageB2C);
		}
		BankLogUtils.printB2CManagerQueryReturnLog(messageB2C);
		return messageB2C;
    }
    
    @Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
